import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * A task information dialog class.This dialog class is just a subclass of the Dialog.
 * 
 * @author zhongnanhai
 *
 */
public class TaskDialog extends Dialog{
	/**The task information that this dialog would return.*/
	private Task task = null;
	/**
	 * The constructor with a default style.
	 * @param parent its parent window
	 */
	public TaskDialog(Shell parent){
		this(parent,SWT.DIALOG_TRIM | SWT.CLOSE | SWT.PRIMARY_MODAL);
	}
	/**
	 * The constructor of the TaskDialog.
	 * 
	 * @param parent its parent shell
	 * @param style its window style
	 */
	public TaskDialog(Shell parent, int style) {
		super(parent,SWT.DIALOG_TRIM | SWT.CLOSE | SWT.PRIMARY_MODAL | style);
		// TODO Auto-generated constructor stub
	}
	/**
	 * Set the task information of this dialog.
	 * @param task the Task object
	 */
	public void setTask(Task task){
		this.task = task;
	}
	/**
	 * The open method which a Dialog class must implement.
	 * 
	 * @return a Task object
	 */
	public Task open(){
		Shell shell = new Shell(getParent(),getStyle());
		if(task == null) shell.setText("Add a new task");
		else shell.setText("Edit "+task.getName());
		createContents(shell);
		shell.pack();
		int x = getParent().getBounds().x + (getParent().getBounds().width - shell.getBounds().width)/2;
		int y = getParent().getBounds().y + (getParent().getBounds().height - shell.getBounds().height)/2;
		shell.setLocation(x,y);
		shell.open();
		Display display = getParent().getDisplay();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
		return task;
	}
	/**
	 * Create all of the contents of this dialog
	 * 
	 * @param shell the dialog window
	 */
	private void createContents(final Shell shell){
		shell.setLayout(new FormLayout());
		int gap = 3;
		//add all necessary controls
		FormData data = new FormData();
		data.right = new FormAttachment(30,0);
		data.top = new FormAttachment(0,gap);
		Label nameLbl = new Label(shell,SWT.RIGHT);
		nameLbl.setText("Task Name:");
		nameLbl.setLayoutData(data);
		data = new FormData();
		data.left = new FormAttachment(nameLbl);
		data.right = new FormAttachment(80,0);
		data.top = new FormAttachment(0,0);
		final Text nameTxt = new Text(shell,SWT.BORDER);
		nameTxt.setLayoutData(data);
		data = new FormData();
		data.right = new FormAttachment(30,0);
		data.top = new FormAttachment(nameTxt,gap);
		Label dscribLbl = new Label(shell,SWT.RIGHT);
		dscribLbl.setLayoutData(data);
		dscribLbl.setText("Task Description:");
		data = new FormData();
		data.left = new FormAttachment(dscribLbl);
		data.right = new FormAttachment(80,0);
		data.top = new FormAttachment(nameTxt);
		final Text dscribTxt = new Text(shell,SWT.BORDER);
		dscribTxt.setLayoutData(data);
		data = new FormData();
		data.right = new FormAttachment(30,0);
		data.top = new FormAttachment(dscribTxt,gap);
		Label sizeLabel = new Label(shell,SWT.RIGHT);
		sizeLabel.setLayoutData(data);
		sizeLabel.setText("Task Size(LOC):");
		data = new FormData();
		data.left = new FormAttachment(sizeLabel);
		data.right = new FormAttachment(80,0);
		data.top = new FormAttachment(dscribTxt);
		final Text sizeTxt = new Text(shell,SWT.BORDER);
		sizeTxt.setLayoutData(data);
		data = new FormData();
		data.right = new FormAttachment(30,0);
		data.top = new FormAttachment(sizeTxt,gap);
		Label needRoleLbl = new Label(shell,SWT.RIGHT);
		needRoleLbl.setText("Need Role:");
		needRoleLbl.setLayoutData(data);
		data = new FormData();
		data.left = new FormAttachment(sizeLabel);
		data.right = new FormAttachment(85,0);
		data.top = new FormAttachment(sizeTxt);
		final Combo rolesList = new Combo(shell,SWT.DROP_DOWN | SWT.READ_ONLY);
		rolesList.setItems(AllBasicData.roles);
		rolesList.setLayoutData(data);
		data = new FormData();
		data.right = new FormAttachment(30,0);
		data.top = new FormAttachment(rolesList,gap);
		Label taskTypeLbl = new Label(shell,SWT.RIGHT);
		taskTypeLbl.setText("Task Type:");
		taskTypeLbl.setLayoutData(data);
		data = new FormData();
		data.left = new FormAttachment(taskTypeLbl);
		data.right = new FormAttachment(85,0);
		data.top = new FormAttachment(rolesList);
		final Combo taskTypesList = new Combo(shell,SWT.DROP_DOWN | SWT.READ_ONLY);
		taskTypesList.setItems(AllBasicData.skills);
		taskTypesList.setLayoutData(data);
		data = new FormData();
		data.left = new FormAttachment(25,0);
		data.top = new FormAttachment(taskTypesList,gap);
		final Button terminalBtn = new Button(shell,SWT.CHECK);
		terminalBtn.setText("Is A Terminal Task");
		terminalBtn.setLayoutData(data);
		data = new FormData();
		data.top = new FormAttachment(terminalBtn,gap);
		data.left = new FormAttachment(10,0);
		data.right = new FormAttachment(35,0);
		data.bottom = new FormAttachment(100,0);
		Button addBtn = new Button(shell,SWT.PUSH);
		addBtn.setLayoutData(data);
		data = new FormData();
		data.top = new FormAttachment(terminalBtn,gap);
		data.left = new FormAttachment(65,0);
		data.right = new FormAttachment(90,0);
		data.bottom = new FormAttachment(100,0);
		Button cancelBtn = new Button(shell,SWT.PUSH);
		cancelBtn.setLayoutData(data);
		cancelBtn.setText("Cancel");
		//Set display different showing text of all the controls in different condition:
		//edit or create?
		if(task == null){//create new task
			nameTxt.setText("");
			dscribTxt.setText("");
			sizeTxt.setText("");
			rolesList.select(-1);
			taskTypesList.select(-1);
			terminalBtn.setSelection(false);
			addBtn.setText("Add");
		}else{//modify a existed task
			nameTxt.setText(task.getName());
			dscribTxt.setText(task.getDescription());
			sizeTxt.setText(task.getSize()+"");
			rolesList.select(task.getRole());
			taskTypesList.select(task.getTaskType());
			terminalBtn.setSelection(task.isTerminal());
			addBtn.setText("Modify");
		}
		//and necessary actions to its corresponding control
		addBtn.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				float size = 0.0f;
				if(nameTxt.getText().trim().isEmpty()) {
					Utilities.showMessage(shell,SWT.ICON_ERROR,"Please input the task name");
					return;
				}else if(dscribTxt.getText().trim().isEmpty()){
					Utilities.showMessage(shell,SWT.ICON_ERROR,"Please input some description about this task");
					return;
				}else if(sizeTxt.getText().trim().isEmpty()){
					Utilities.showMessage(shell,SWT.ICON_ERROR,"Please set the size of this task");
					return;
				}else{
					if(!sizeTxt.getText().trim().isEmpty()){
						try{
							size = Float.parseFloat(sizeTxt.getText().trim());
						}catch(Exception ept){
							Utilities.showMessage(shell,SWT.ICON_ERROR,"Please input the correct format of the task size");
							return;
						}
					}
					if(rolesList.getSelectionIndex()<0){
						Utilities.showMessage(shell,SWT.ICON_ERROR,"Please select the type of this task");
						return;
					}
					if(taskTypesList.getSelectionIndex()<0){
						Utilities.showMessage(shell,SWT.ICON_ERROR,"Please select the role that this task needs");
						return;
					}
				} 
				if(terminalBtn.getSelection()){
					Utilities.showMessage(shell,SWT.ICON_WARNING,"Are you sure to set this task as a terminal task?\n" +
							"Note: a terminal task is the task denoting the finish of a project.There should " +
							"be only one terminal task,\nso if you set this as the terminal,the preliminary terminal" +
							" status of a task will be removed");
				}
				boolean isNew = true;
				if(task == null){
					isNew = true;
					task = new Task();
				}else{
					isNew = false;
				}
				task.setName(nameTxt.getText().trim());
				task.setDescription(dscribTxt.getText().trim());
				task.setSize(size);
				task.setTaskType(taskTypesList.getSelectionIndex());
				task.setRole(rolesList.getSelectionIndex());
				task.setTerminal(terminalBtn.getSelection());
				if(isNew)
					Utilities.showMessage(shell,SWT.ICON_INFORMATION,"A task has been created.");
				else
					Utilities.showMessage(shell,SWT.ICON_INFORMATION,"The task has been modified.");
				shell.dispose();
			}
		});
		cancelBtn.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				shell.dispose();
			}
		});
	}
	public static void main(String[] args) throws Exception{
		Display display = new Display();
		Shell shell = new Shell(display);
		new TaskDialog(shell,SWT.DIALOG_TRIM).open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
